package cn.edu.scau.cmi.wuzewei.client;

import cn.edu.scau.cmi.wuzewei.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.wuzewei.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.wuzewei.composite.consistent.ConsistentLeaf;
import cn.edu.scau.cmi.wuzewei.composite.safe.SafeComponent;
import cn.edu.scau.cmi.wuzewei.composite.safe.SafeComposite;
import cn.edu.scau.cmi.wuzewei.composite.safe.SafeLeaf;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
import javafx.collections.FXCollections;

public class CompositeClient extends Application {
	@Override
	public void start(Stage primaryStage) throws Exception {

		Group group = new Group();

		SafeComposite safeComposite1 = new SafeComposite(null, "Dir-安全形式的组合模式");
		TreeItem<SafeComponent> rootItem1 = new TreeItem<>(safeComposite1);
		rootItem1.setExpanded(true);
		for (int i = 1; i < 3; i++) {
			SafeComposite tmpSafeComposite = new SafeComposite(safeComposite1, "Dir-目录" + i);
			TreeItem<SafeComponent> item = new TreeItem<>(tmpSafeComposite);
			item.setExpanded(true);
			rootItem1.getChildren().add(item);
		}
		TreeView<SafeComponent> tree = new TreeView<>(rootItem1);
		tree.getSelectionModel().select(1);
		tree.setEditable(true);
		group.getChildren().add(tree);

		ConsistentComposite ConsistentComposite = new ConsistentComposite(null, "Dir-透明形式的组合模式");
		TreeItem<ConsistentComponent> rootItem2 = new TreeItem<>(ConsistentComposite);
		rootItem2.setExpanded(true);
		for (int i = 1; i < 3; i++) {
			ConsistentComposite tmpConsistentComposite = new ConsistentComposite(ConsistentComposite, "Dir-目录" + i);
			TreeItem<ConsistentComponent> item = new TreeItem<>(tmpConsistentComposite);
			item.setExpanded(true);
			rootItem2.getChildren().add(item);
		}
		TreeView<ConsistentComponent> tree2 = new TreeView<>(rootItem2);
		tree2.getSelectionModel().select(1);
		tree2.setEditable(true);
		tree2.setLayoutX(450);
		group.getChildren().add(tree2);

		Label label1 = new Label("请选择形式：");
		label1.setLayoutX(270);
		label1.setLayoutY(50);
		group.getChildren().add(label1);

		@SuppressWarnings({ "unchecked", "rawtypes" })
		ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList("安全形式的组合模式", "透明形式的组合模式"));
		cb.setLayoutX(280);
		cb.setLayoutY(70);
		cb.getSelectionModel().select(0);
		group.getChildren().add(cb);

		Label label = new Label("请选择目录项以创建：");
		label.setLayoutX(270);
		label.setLayoutY(100);
		group.getChildren().add(label);

		Label label2 = new Label();
		label2.setLayoutX(270);
		label2.setLayoutY(210);
		group.getChildren().add(label2);

		TextField textField = new TextField();
		textField.setText("name");
		textField.setLayoutX(280);
		textField.setLayoutY(130);
		group.getChildren().add(textField);

		Button button1 = new Button("创建目录");
		button1.setLayoutX(280);
		button1.setLayoutY(170);
		button1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if (cb.getSelectionModel().getSelectedIndex() == 0) {
					TreeItem<SafeComponent> selectItem = tree.getSelectionModel().getSelectedItem();
					if (selectItem.getValue().toString().startsWith("Dir-")) {
						SafeComposite safeComposite = new SafeComposite(selectItem.getValue(),
								"Dir-" + textField.getText());
						TreeItem<SafeComponent> item = new TreeItem<>(safeComposite);
						item.setExpanded(true);
						selectItem.getChildren().add(item);
						label2.setText("");
					} else {
						label2.setText("非目录下无法创建!!!");
					}
				} else {
					TreeItem<ConsistentComponent> selectItem = tree2.getSelectionModel().getSelectedItem();
					if (selectItem.getValue().toString().startsWith("Dir-")) {
						ConsistentComposite safeComposite = new ConsistentComposite(selectItem.getValue(),
								"Dir-" + textField.getText());
						TreeItem<ConsistentComponent> item = new TreeItem<>(safeComposite);
						item.setExpanded(true);
						selectItem.getChildren().add(item);
						label2.setText("");
					} else {
						label2.setText("非目录下无法创建!!!");
					}
				}
			}
		});
		group.getChildren().add(button1);

		Button button2 = new Button("创建文件");
		button2.setLayoutX(350);
		button2.setLayoutY(170);
		button2.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if (cb.getSelectionModel().getSelectedIndex() == 0) {
					TreeItem<SafeComponent> selectItem = tree.getSelectionModel().getSelectedItem();
					if (selectItem.getValue().toString().startsWith("Dir-")) {
						SafeLeaf safeLeaf = new SafeLeaf(selectItem.getValue(), textField.getText());
						TreeItem<SafeComponent> item = new TreeItem<>(safeLeaf);
						item.setExpanded(true);
						selectItem.getChildren().add(item);
						label2.setText("");
					} else {
						label2.setText("非目录下无法创建!!!");
					}
				} else {
					TreeItem<ConsistentComponent> selectItem = tree2.getSelectionModel().getSelectedItem();
					if (selectItem.getValue().toString().startsWith("Dir-")) {
						ConsistentLeaf safeComposite = new ConsistentLeaf(selectItem.getValue(), textField.getText());
						TreeItem<ConsistentComponent> item = new TreeItem<>(safeComposite);
						item.setExpanded(true);
						selectItem.getChildren().add(item);
						label2.setText("");
					} else {
						label2.setText("非目录下无法创建!!!");
					}
				}

			}
		});
		group.getChildren().add(button2);

		Scene scene = new Scene(group);
		primaryStage.setTitle("实验五：组合模式");
		primaryStage.setWidth(700);
		primaryStage.setHeight(400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}

}
